package com.rainng.coursesystem.service.member;

import com.rainng.coursesystem.manager.OptionManager;
import com.rainng.coursesystem.manager.member.EventSelectManager;
import com.rainng.coursesystem.model.bo.MemberEventSelectItemBO;
import com.rainng.coursesystem.model.entity.EventEntity;
import com.rainng.coursesystem.model.entity.MemberEventEntity;
import com.rainng.coursesystem.model.entity.MemberEntity;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.model.vo.response.table.MemberEventSelectItemVO;
import com.rainng.coursesystem.service.BaseService;
import com.rainng.coursesystem.util.LessonTimeConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EventSelectService extends BaseService {
    private final EventSelectManager manager;
    private final OptionManager optionManager;
    private final LessonTimeConverter lessonTimeConverter;

    public EventSelectService(EventSelectManager manager, OptionManager optionManager, LessonTimeConverter lessonTimeConverter) {
        this.manager = manager;
        this.optionManager = optionManager;
        this.lessonTimeConverter = lessonTimeConverter;
    }

    public ResultVO getPageCount(String eventName, String leaderName) {
        Integer memberId = getUserId();
        return result(manager.getPageCount(memberId, eventName, leaderName));
    }

    public ResultVO getPage(Integer index, String eventName, String leaderName) {
        Integer memberId = getUserId();

        List<MemberEventSelectItemBO> boList = manager.getPage(index, memberId, eventName, leaderName);
        List<MemberEventSelectItemVO> voList = new ArrayList<>(boList.size());

        for (MemberEventSelectItemBO bo : boList) {
            MemberEventSelectItemVO vo = new MemberEventSelectItemVO();
            BeanUtils.copyProperties(bo, vo);
            vo.setTime(lessonTimeConverter.covertTimePart(bo.getTime()));
            voList.add(vo);
        }

        return result(voList);
    }

    public ResultVO create(Integer eventId) {
        Integer memberId = getUserId();

        if (!optionManager.getAllowMemberSelect()) {
            return failedResult("现在不是选项目时间!");
        }
        MemberEntity member = manager.getMemberById(memberId);
        EventEntity event = manager.getEventById(eventId);
        if (member == null) {
            return failedResult("团员Id:" + memberId + "不存在!");
        }
        if (event == null) {
            return failedResult("项目Id:" + eventId + "不存在!");
        }
        if (!manager.inSameArea(eventId, memberId)) {
            return failedResult("团员不能选择非此地区的项目!");
        }
        if (event.getSelectedCount() >= event.getMaxSize()) {
            return failedResult("项目容量已满!");
        }
        if (manager.getMemberEventByEventIdAndMemberId(eventId, memberId) != null) {
            return failedResult("团员已选此项目!");
        }
        if (!manager.getMemberBudgetById(member.getId()).equals(event.getNumber())) {
            return failedResult("最大团员数量不匹配");
        }
        String timePart = splitTimePart(event.getTime());
        if (manager.countMemberEventSelectedByTimePart(memberId, timePart) > 0) {
            return failedResult("项目时间冲突!");
        }

        MemberEventEntity memberEvent = new MemberEventEntity();
        memberEvent.setEventId(eventId);
        memberEvent.setMemberId(memberId);
        manager.create(memberEvent);

        return result("选项目成功");
    }

    private String splitTimePart(String time) {
        String[] spilt = time.split("-");
        return spilt[0] + "-" + spilt[1];
    }
}
